﻿using HardcoreGame.Common.Abstract;
using HardcoreGame.Common.Systems;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.Utilities;
namespace HardcoreGame.Content.NPCs.Enemies.Seasonsa.Summers
{
    public class Mosquito1 : ModNPC
    {
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = 4;
        }
        public override void SetDefaults()
        {
            NPC.width = 20;
            NPC.height = 20;
            AnimationType = 210;
            NPC.aiStyle = 14;
            NPC.damage = 2;
            NPC.defense = 3;
            NPC.lifeMax = 5;
            NPC.noGravity = true;
            NPC.noTileCollide = false;
            NPC.knockBackResist = 1f;
            NPC.npcSlots = 0.75f;
            SoundStyle soundStyle = SoundID.NPCHit32;
            NPC.HitSound = new SoundStyle?(soundStyle.WithPitchOffset(0.2f).WithVolumeScale(0.5f));
            soundStyle = SoundID.NPCDeath35;
            NPC.DeathSound = new SoundStyle?(soundStyle.WithPitchOffset(0.2f).WithVolumeScale(0.5f));
            NPC.value = 15f;
            NPC.scale = Main.rand.NextFloat(0.2f, 0.6f);
            SpawnModBiomes = [ModContent.GetInstance<Biomes.Summer>().Type];
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            SeasonSystem seasonSystem = ModContent.GetInstance<SeasonSystem>();
            Season currentSeason = seasonSystem.currentSeason;
            if (currentSeason is Common.Cores.Seasons.Summer)
            {
                if (spawnInfo.Player.ZoneOverworldHeight && !Main.dayTime)
                {
                    float baseChance = SpawnCondition.OverworldDay.Chance * 0.1f;
                    if (IsNearWater(spawnInfo.Player))
                    {
                        return baseChance * 3;
                    }
                    return baseChance;
                }
            }
            return 0f;
        }
        private static bool IsNearWater(Player player)
        {
            int checkRange = 5;
            int left = (int)(player.position.X / 16) - checkRange;
            int top = (int)(player.position.Y / 16) - checkRange;
            int right = (int)(player.position.X / 16) + checkRange;
            int bottom = (int)(player.position.Y / 16) + checkRange;
            for (int x = left; x <= right; x++)
            {
                for (int y = top; y <= bottom; y++)
                {
                    if (x >= 0 && x < Main.maxTilesX && y >= 0 && y < Main.maxTilesY)
                    {
                        Tile tile = Main.tile[x, y];
                        if (tile.HasTile && tile.LiquidAmount > 0 && tile.LiquidType == LiquidID.Water)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange(
            [
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Times.NightTime,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.Bestiary.Mosquito")
            ]);
        }
        public override void AI()
        {
            NPC.TargetClosest(true);
            if (Vector2.Distance(NPC.velocity, Vector2.Zero) > 2f)
            {
                NPC.velocity *= 0.98f;
            }
            if (Vector2.Distance(Main.player[NPC.target].Center, NPC.Center) < 160f)
            {
                NPC.aiStyle = 2;
                NPC.velocity *= 0.95f;
            }
            else
            {
                NPC.aiStyle = 14;
            }
            if (NPC.soundDelay == 0)
            {
                SoundStyle soundStyle = ((SoundStyle)SoundID.Zombie46).WithPitchOffset(0.25f).WithVolumeScale(0.9f);
                soundStyle.PitchVariance = 0.1f;
                SoundEngine.PlaySound(soundStyle, new Vector2?(NPC.Center), null);
                NPC.soundDelay = Main.rand.Next(300, 420);
            }
            if (NPC.wet)
            {
                NPC npc = NPC;
                npc.velocity.Y -= 0.2f;
                NPC.life--;
            }
        }
        public override void OnHitPlayer(Player target, Player.HurtInfo hurtInfo)
        {
            SoundStyle soundStyle = ((SoundStyle)SoundID.Item17).WithVolumeScale(0.5f);
            soundStyle = ((SoundStyle)SoundID.Item17).WithPitchOffset(0.25f);
            SoundEngine.PlaySound(SoundID.Item17, new Vector2?(target.Center), null);
            NPCLifeSuck(NPC, hurtInfo.Damage, 1, target.Center);
            target.AddBuff(30, 300, true, false);
            if (Main.expertMode && Main.rand.NextBool(10))
            {
                target.AddBuff(148, Main.rand.Next(1800, 5400), true, false);
            }
        }
        public override void OnHitNPC(NPC target, NPC.HitInfo hit)
        {
            NPCLifeSuck(NPC, hit.Damage, 1, target.Center);
            target.AddBuff(30, 300, false);
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (Main.netMode == NetmodeID.Server)
            {
                return;
            }
            if (NPC.life <= 0)
            {
                for (int f = 0; f < 5; f++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Cloud, 2f * hit.HitDirection, Main.rand.NextFloat(-2f, 2f), 100, Utils.SelectRandom(Main.rand, new Color[]
                    {
                        Color.Red,
                        Color.Brown,
                        Color.DarkGreen
                    }), 1.25f);
                }
            }
        }
        public static void NPCLifeSuck(NPC npc, int dmg, byte style = 0, Vector2 targetPos = default)
        {
            npc.life += Utils.Clamp(dmg, 1, npc.lifeMax - npc.life);
            Color textColor = CombatText.HealLife;
            if (style == 1 && targetPos != Vector2.Zero)
            {
                BurstDusts(targetPos, 5, Vector2.Normalize(npc.Center - targetPos), 1.25f, 7, 25, true, new byte?(80), null, 0);
            }
            CombatText.NewText(new Rectangle((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height), textColor, dmg.ToString("0"), false, false);
        }
        public static void BurstDusts(Vector2 position, int dustType, Vector2 velocity, float dustScale = 1f, byte catastrophy = 5, byte magnitude = 10, bool noGravity = true, byte? alpha = null, Color? color = null, byte fadeIn = 0)
        {
            if (alpha == null)
            {
                alpha = new byte?(105);
            }
            if (color == null)
            {
                color = new Color?(default);
            }
            for (byte d = 0; d < catastrophy; d += 1)
            {
                short dust = (short)Dust.NewDust(position, 0, 0, dustType, velocity.X, velocity.Y, alpha.Value, color.Value, dustScale + Main.rand.NextFloat(0f, 0.25f));
                Main.dust[dust].noGravity = noGravity;
                if (magnitude != 0)
                {
                    Main.dust[dust].velocity *= 1f + magnitude / 20f;
                }
                if (fadeIn != 0)
                {
                    Main.dust[dust].fadeIn = fadeIn / 50f;
                }
            }
        }
    }
}
